package com.sihai.ehr.service.impl;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.TrainPlanEmployeeTarget;
import com.sihai.ehr.service.TrainPlanEmployeeTargetService;

/**
 * 培训人员对旬服务实现
 * 
 * @author henry
 * 
 */
@Service("trainPlanEmployeeTargetService")
public class TrainPlanEmployeeTargetServiceImpl implements TrainPlanEmployeeTargetService {
	@Resource
	DataAccessor dataAccessor;

	@Override
	public TrainPlanEmployeeTarget getTrainPlanEmployeeTarget(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(TrainPlanEmployeeTarget.class, id);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取培训员工对象明细数据库发生异常】【异常说明：+" + e.getMessage() + "】【发生地点：TrainPlanEmployeeTargetServiceImpl.getTrainPlanEmployeeTarget;参数id=" + id
					+ " 】");
		}
	}

	@Override
	public LimitedList<TrainPlanEmployeeTarget> getTrainPlanEmployeeTargets(final Long planId, Limit limit, Sorter sorter) {
		try {
			return dataAccessor.findByQuery(TrainPlanEmployeeTarget.class, new QueryCreator() {

				@Override
				public Query createQuery(Session session) {
					DynamicQuery dynamicQuery = new DynamicQuery("from TrainPlanEmployeeTarget t where 1 <> 2");
					dynamicQuery.append("and t.trainPlan.id=" + planId);

					return dynamicQuery.build(session);
				}
			}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取培训员工对象时数据库发生异常】【异常说明：+" + e.getMessage() + "】【发生地点：TrainPlanEmployeeTargetServiceImpl.getTrainPlanEmployeeTargets;参数planId="
					+ planId + "】");
		}
	}
	
	
	public Long getTrainPlanEmployeeTargetNum(final Long planId) {
		try {
			return dataAccessor.findUnique(Long.class, new QueryCreator() {

				@Override
				public Query createQuery(Session session) {
					DynamicQuery dynamicQuery = new DynamicQuery("select count(t.id) from TrainPlanEmployeeTarget t where 1 <> 2");
					dynamicQuery.append(" and t.trainPlan.id=" + planId);

					return dynamicQuery.build(session);
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取培训员工对象时数据库发生异常】【异常说明：+" + e.getMessage() + "】【发生地点：TrainPlanEmployeeTargetServiceImpl.getTrainPlanEmployeeTargetNum;参数planId="
					+ planId + "】");
		}
	}

	@Override
	public void save(TrainPlanEmployeeTarget employeeTarget) {
		if (employeeTarget == null || employeeTarget.getTrainPlan() == null) {
			throw new RuntimeException("【employeeTarget对象参数异常】");
		}
		try {
			dataAccessor.saveOrUpdate(employeeTarget);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保培训员工对象对象到数据库发生异常】【异常说明：" + e.getMessage() + "】【发生地点：TrainPlanEmployeeTargetServiceImpl.save】");
		}
	}

	@Override
	public void deleteTrainPlanEmployeeTarget(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat.format("delete from TrainPlanEmployeeTarget t where t.id in ({0})", ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：移出培训员工对象信息数据库发生异常】【异常说明：" + e.getMessage() + "】【发生地点：TrainPlanEmployeeTargetServiceImpl.deleteTrainPlanEmployeeTarget】");
		}
	}

	@Override
	public void deleteTrainPlanEmployeeTargetByPlanId(final Long planId) {
		if (planId == null || planId < 0) {
			throw new RuntimeException("【参数异常：planId为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat.format("delete from TrainPlanEmployeeTarget t where t.trainPlan.id={0})", planId));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：移出培训员工对象信息数据库发生异常】【异常说明：" + e.getMessage() + "】【发生地点：TrainPlanEmployeeTargetServiceImpl.deleteTrainPlanEmployeeTargetByPlanId】");
		}
	}

	@Override
	public void deleteTrainPlanEmployeeTargetByPlanIds(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat.format("delete from TrainPlanEmployeeTarget t where t.trainPlan.id in ({0})", ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：移出培训员工对象信息数据库发生异常】【异常说明：" + e.getMessage() + "】【发生地点：TrainPlanDutyTargetServiceImpl.deleteTrainPlanDutyTarget】");
		}
	}

}
